package com.sservice.web.controller.admin;

import com.sservice.banfu.e.SalePerson;
import com.sservice.banfu.mapper.SalePersonMapper;
import com.sservice.banfu.service.SalePersonService1;
import com.sservice.banfu.vo.SalePersonVo;
import com.sservice.base.SameUrlData;
import com.sservice.base.Utils;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.common.Constants;
import com.sservice.model.vo.UserWxVo;
import com.sservice.service.service.ExportService;
import com.sservice.service.service.IInfoService;
import com.sservice.service.service.ISalePersonService;
import com.sservice.service.service.IUserWxService;
import com.sservice.urp.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/saleperson")
public class SalePersonController {
    @Autowired
    IUserWxService iUserWxService;
    @Autowired
    ExportService exportService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ISalePersonService depService;
    @Autowired
    private SalePersonMapper salePersonMapper;
    @Autowired
    private SalePersonService1 salePersonService1;
    @Autowired
    private IInfoService iInfoService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<SalePersonVo> get(SalePerson arg) {
        SalePersonVo service_res = salePersonService1.selectByPrimaryKey(arg.getId());
        BaseResult<SalePersonVo> res = new BaseResult<SalePersonVo>(true, service_res);
        return res;
    }

    /***
     * 修改数据
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(SalePerson arg, HttpSession session) {
//        SalePerson sale = (SalePerson) session.getAttribute("current");
//        BaseResult<Integer> res;
//        if (sale != null) {
//            arg.setValid(null);
//            arg.setId(sale.getId());
//            int service_res = depService.updateByPrimaryKeySelective(arg);
//            res = new BaseResult<Integer>(true, service_res);
//            SalePerson sp = depService.selectByPrimaryKey(arg.getId());
//            session.setAttribute("current", sp);
//
//        } else {
//            res = new BaseResult<Integer>(false, "请登录!", 401);
//        }
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/valid", method = RequestMethod.POST)
    public BaseResult<Object> valid(SalePerson arg, HttpSession session) {
        User user = (User) session.getAttribute(Constants.USER);
        BaseResult<Object> res;
        if (user != null) {
            SalePersonVo sp = salePersonService1.selectByPrimaryKey(arg.getId());
            if (sp != null && "0".equals(sp.getValid()) && "1".equals(arg.getValid())) {
                int service_res = depService.updateByPrimaryKeySelective(arg);
                res = new BaseResult(true, service_res);
            } else {
                res = new BaseResult(false, "非法操作!", 403);
            }
        } else {
            res = new BaseResult(false, "请登录!", 401);
        }
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<SalePerson> query(SalePerson arg) {
        List<SalePerson> list = depService.queryByPage(arg);
        int total = depService.queryCount(null);
        //Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
//        res.setPage(page);
        return res;
    }

    /**
     * 注册用户
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryUserWx", method = RequestMethod.GET)
    public ListResult<UserWxVo> queryUserWx(UserWxVo arg) {
        List<UserWxVo> list = iUserWxService.queryCommonByPage(arg);
        int total = iUserWxService.countCommonByPage(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<UserWxVo> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    @SameUrlData
    @RequestMapping(value = "/exportUserWx", method = RequestMethod.GET)
    public OutputStream exportUserWx(UserWxVo arg, HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException {
        arg.setSize(Integer.MAX_VALUE);
        ListResult lr = queryUserWx(arg);
        List<UserWxVo> list = (List<UserWxVo>) lr.getList();
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/binary;charset=" + "UTF-8");
        String fileName = Utils.processFileName(request, "授权用户");
        response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式
        response.setCharacterEncoding("UTF-8");
        String hql = "授权用户"; // sheet 名称
        List<Map> lm = new ArrayList<Map>();
        for (int i = 0; i < list.size(); i++) {
            Map map = new LinkedHashMap();
            UserWxVo ov = list.get(i);
            map.put("昵称", ov.getNickName());
            map.put("电话", ov.getPhone());
            map.put("城市", ov.getCity());
            map.put("国家", ov.getCountry());

            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            map.put("注册时间", ov.getCreateAt() != null ? sdf1.format(ov.getCreateAt()) : null);
            lm.add(map);
        }
        return exportService.exportExcel(hql, lm, outputStream);
    }

    /**
     * 销售交易额统计
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statistic_query", method = RequestMethod.GET)
    public ListResult<SalePerson> statisticQuery(SalePerson arg) {
        List<SalePerson> list = depService.querySaleCashByPage(arg);
        int total = depService.querySaleCashCount(arg);
//        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
//        res.setPage(page);
        return res;
    }


    @ResponseBody
    @RequestMapping(value = "/querySaleManager", method = RequestMethod.GET)
    public ListResult<SalePerson> querySaleManager(SalePersonVo arg) {
        List<SalePerson> list = depService.querySaleManager(arg);
        int total = depService.querySaleManagerCount(null);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/mySales", method = RequestMethod.GET)
    public ListResult<SalePerson> queryMySales(SalePerson arg) {
        List<SalePerson> list = depService.queryByUserIdAndPage(arg);
        int total = depService.queryCount(null);
//        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
//        res.setPage(page);
        return res;
    }


}
